package com.tencent.qf.team.controller;

import com.alibaba.fastjson.JSON;
import com.tencent.qf.common.pojo.HttpResult;
import com.tencent.qf.mybatis.pojo.PageRequest;
import com.tencent.qf.mybatis.pojo.PageResult;
import com.tencent.qf.team.dto.request.GuiderOrderCpsMultiUserstatisticRequest;
import com.tencent.qf.team.dto.request.GuiderOrderCpsPaginateRequest;
import com.tencent.qf.team.dto.request.GuiderOrderCpsPersonalTotalRequest;
import com.tencent.qf.team.dto.request.GuiderOrderGoodsCpsPaginateRequest;
import com.tencent.qf.team.dto.response.GuiderOrderCpsMultiUserstatisticResponse;
import com.tencent.qf.team.dto.response.GuiderOrderCpsPaginateResponse;
import com.tencent.qf.team.dto.response.GuiderOrderCpsPersonalTotalResponse;
import com.tencent.qf.team.dto.response.GuiderOrderGoodsCpsPaginateResponse;
import com.tencent.qf.team.dto.response.PerformanceSummaryPersonalListResponse;
import com.tencent.qf.team.service.GuiderOrderCpsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@Api(value = "收益统计", tags = "收益统计", protocols = "http")
public class GuiderOrderCpsController {

    @Autowired
    private GuiderOrderCpsService guiderOrderCpsService;

    @PostMapping("/api/team/guiderOrderCpsPersonalTotal")
    @ApiOperation("小B(包含下属)维度-个人订单总计")
    public HttpResult<GuiderOrderCpsPersonalTotalResponse> guiderOrderCpsPersonalTotal(@RequestBody GuiderOrderCpsPersonalTotalRequest request) {
        log.info("小B(包含下属)维度-个人订单总计，入参：{}", JSON.toJSONString(request));
        GuiderOrderCpsPersonalTotalResponse data = guiderOrderCpsService.guiderOrderCpsPersonalTotal(request);
        HttpResult<GuiderOrderCpsPersonalTotalResponse> result = HttpResult.success(data);
        log.info("小B(包含下属)维度-个人订单总计，出参：{}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/api/team/guiderOrderCpsPaginate")
    @ApiOperation("小B(包含下属)维度-订单分页获取订单明细")
    public HttpResult<PageResult<GuiderOrderCpsPaginateResponse>> guiderOrderCpsPaginate(@RequestBody PageRequest<GuiderOrderCpsPaginateRequest> request) {
        log.info("小B(包含下属)维度-订单分页获取订单明细，入参：{}", JSON.toJSONString(request));
        PageResult<GuiderOrderCpsPaginateResponse> pageResult = guiderOrderCpsService.guiderOrderCpsPaginate(request);
        HttpResult<PageResult<GuiderOrderCpsPaginateResponse>> result = HttpResult.success(pageResult);
        log.info("小B(包含下属)维度-订单分页获取订单明细，出参：{}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/api/team/guiderOrderCpsMultiUserstatistic")
    @ApiOperation("运营平台_小b订单业绩汇总")
    public HttpResult<List<GuiderOrderCpsMultiUserstatisticResponse>> guiderOrderCpsMultiUserstatistic(@RequestBody GuiderOrderCpsMultiUserstatisticRequest request) {
        log.info("运营平台_小b订单业绩汇总，入参：{}", JSON.toJSONString(request));
        List<GuiderOrderCpsMultiUserstatisticResponse> data = guiderOrderCpsService.guiderOrderCpsMultiUserstatistic(request);
        HttpResult<List<GuiderOrderCpsMultiUserstatisticResponse>> result = HttpResult.success(data);
        log.info("运营平台_小b订单业绩汇总，出参：{}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/api/team/guiderOrderGoodsCpsPaginate")
    @ApiOperation("小B(包含下属)维度-订单商品分页")
    public HttpResult<PageResult<GuiderOrderGoodsCpsPaginateResponse>> guiderOrderGoodsCpsPaginate(@RequestBody PageRequest<GuiderOrderGoodsCpsPaginateRequest> request) {
        log.info("小B(包含下属)维度-订单商品分页，入参：{}", JSON.toJSONString(request));

        GuiderOrderGoodsCpsPaginateRequest requestData = request.getData();
        List<String> orderFields = Arrays.asList(new String[]{"store_name", "external_sku_id", "sku_name_chinese", "external_spu_id", "spu_name_chinese",
                "primary_image_url", "category_level1_name", "goodsAmount", "avgGoodsAmount", "buyerCount", "payGoodsSum", "returnedGoodsSum", "gmv"});
        List<String> orderSorts = Arrays.asList(new String[]{"desc", "asc"});

        HttpResult<PageResult<GuiderOrderGoodsCpsPaginateResponse>> result;
        if (!orderFields.contains(requestData.getOrderField())) {
            result = HttpResult.error(500, "排序字段格式错误");
        } else if (!orderSorts.contains(requestData.getOrderType())) {
            result = HttpResult.error(500, "排序方式格式错误");
        } else {
            PageResult<GuiderOrderGoodsCpsPaginateResponse> pageResult = guiderOrderCpsService.guiderOrderGoodsCpsPaginate(request);
            result = HttpResult.success(pageResult);
        }

        log.info("小B(包含下属)维度-订单商品分页，出参：{}", JSON.toJSONString(result));
        return result;
    }
}
